/*
 * FreeModbus Libary: RT-Thread Port
 * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * File: $Id: port.c,v 1.60 2015/02/01 9:18:05 Armink $
 */

/* ----------------------- System includes --------------------------------*/

/* ----------------------- Modbus includes ----------------------------------*/
#include "port.h"
#include "mb.h"
#include "mbport.h"
#include "mbutils.h"
/* ----------------------- Variables ----------------------------------------*/
//uint16_t usRegInputBuf[REG_INPUT_NREGS] = {0x8001, 0x7002, 0x6003, 0x5004, 0x4005, 0x3006, 0x2007, 0x1008};
//uint16_t usRegHoldingBuf[REG_HOLDING_NREGS] = {0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008};
//uint8_t ucRegCoilsBuf[REG_COILS_SIZE] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
//uint8_t ucRegDiscreteBuf[REG_DISCRETE_SIZE] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
/* ----------------------- Start implementation -----------------------------*/

void EnterCriticalSection(void)
{
  __disable_irq();
}

void ExitCriticalSection(void)
{
  __enable_irq();
}

//eMBErrorCode eMBRegInputCB(UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNRegs)
//{
//  eMBErrorCode eStatus = MB_ENOERR;
//  int iRegIndex;

//  if ((usAddress >= REG_INPUT_START) && (usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS))
//  {
//    iRegIndex = (int)(usAddress - REG_INPUT_START);
//    while (usNRegs > 0)
//    {
//      *pucRegBuffer++ =
//          (unsigned char)(usRegInputBuf[iRegIndex] >> 8);
//      *pucRegBuffer++ =
//          (unsigned char)(usRegInputBuf[iRegIndex] & 0xFF);
//      iRegIndex++;
//      usNRegs--;
//    }
//  }
//  else
//  {
//    eStatus = MB_ENOREG;
//  }

//  return eStatus;
//}

//eMBErrorCode eMBRegHoldingCB(UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNRegs,
//                             eMBRegisterMode eMode)
//{
//  eMBErrorCode eStatus = MB_ENOERR;
//  int iRegIndex;

//  if (((int16_t)usAddress >= REG_HOLDING_START) && (usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS))
//  {
//    iRegIndex = (int)(usAddress - REG_HOLDING_START);
//    switch (eMode)
//    {
//    case MB_REG_READ:
//      while (usNRegs > 0)
//      {
//        *pucRegBuffer++ = (unsigned char)(usRegHoldingBuf[iRegIndex] >> 8);
//        *pucRegBuffer++ = (unsigned char)(usRegHoldingBuf[iRegIndex] & 0xFF);
//        iRegIndex++;
//        usNRegs--;
//      }
//      break;

//    case MB_REG_WRITE:
//      while (usNRegs > 0)
//      {
//        usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
//        usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
//        iRegIndex++;
//        usNRegs--;
//      }
//      break;
//    }
//  }
//  else
//  {
//    eStatus = MB_ENOREG;
//  }
//  return eStatus;
//}

//eMBErrorCode eMBRegCoilsCB(UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNCoils,
//                           eMBRegisterMode eMode)
//{
//  eMBErrorCode eStatus = MB_ENOERR;

//  int16_t iNCoils = (int16_t)usNCoils;

//  int16_t usBitOffset;

//  if (((int16_t)usAddress >= REG_COILS_START) &&
//      (usAddress + usNCoils <= REG_COILS_START + REG_COILS_SIZE))
//  {

//    usBitOffset = (int16_t)(usAddress - REG_COILS_START);
//    switch (eMode)
//    {

//    case MB_REG_READ:
//      while (iNCoils > 0)
//      {
//        *pucRegBuffer++ = xMBUtilGetBits(ucRegCoilsBuf, usBitOffset,
//                                         (uint8_t)(iNCoils > 8 ? 8 : iNCoils));
//        iNCoils -= 8;
//        usBitOffset += 8;
//      }
//      break;

//    case MB_REG_WRITE:
//      while (iNCoils > 0)
//      {
//        xMBUtilSetBits(ucRegCoilsBuf, usBitOffset,
//                       (uint8_t)(iNCoils > 8 ? 8 : iNCoils),
//                       *pucRegBuffer++);
//        iNCoils -= 8;
//      }
//      break;
//    }
//  }
//  else
//  {
//    eStatus = MB_ENOREG;
//  }
//  return eStatus;
//}

//eMBErrorCode eMBRegDiscreteCB(UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNDiscrete)
//{
//  eMBErrorCode eStatus = MB_ENOERR;

//  int16_t iNDiscrete = (int16_t)usNDiscrete;

//  uint16_t usBitOffset;

//  if (((int16_t)usAddress >= REG_DISCRETE_START) &&
//      (usAddress + usNDiscrete <= REG_DISCRETE_START + REG_DISCRETE_SIZE))
//  {
//    usBitOffset = (uint16_t)(usAddress - REG_DISCRETE_START);

//    while (iNDiscrete > 0)
//    {
//      *pucRegBuffer++ = xMBUtilGetBits(ucRegDiscreteBuf, usBitOffset,
//                                       (uint8_t)(iNDiscrete > 8 ? 8 : iNDiscrete));
//      iNDiscrete -= 8;
//      usBitOffset += 8;
//    }
//  }
//  else
//  {
//    eStatus = MB_ENOREG;
//  }
//  return eStatus;
//}
